//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.ysy.tz.core.engine;

import com.ysy.tz.core.annotation.ShowLoading;
import com.ysy.tz.core.engine.CustomerFXMLLoader.SupportedType;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.event.Event;
import javafx.event.EventHandler;
import sun.reflect.misc.MethodUtil;

public class CustomerMethodEventHandler<T extends Event> implements EventHandler<T> {
    private final MethodHandler handler;

    public CustomerMethodEventHandler(MethodHandler handler) {
        this.handler = handler;
    }

    public void handle(T event) {
        this.handler.invoke(event);
    }

    public static class MethodHandler {
        private final Object controller;
        private final Method method;
        public final CustomerFXMLLoader.SupportedType type;

        public MethodHandler(Object controller, Method method, CustomerFXMLLoader.SupportedType type) {
            this.method = method;
            this.controller = controller;
            this.type = type;
        }

        public void invoke(final Object... params) {
            if (this.method.getAnnotation(ShowLoading.class) != null) {
                ApplicationContext.showLoadingPane();
                Task task = new Task() {
                    protected Object call() throws Exception {
                        MethodHandler.this.realInvoke(params);
                        Platform.runLater(new Runnable() {
                            public void run() {
                                ApplicationContext.hideLoadingPane();
                            }
                        });
                        return null;
                    }
                };
                ApplicationContext.executor.execute(task);
            } else {
                this.realInvoke(params);
            }

        }

        private void realInvoke(Object... params) {
            try {
                if (this.type != SupportedType.PARAMETERLESS) {
                    MethodUtil.invoke(this.method, this.controller, params);
                } else {
                    MethodUtil.invoke(this.method, this.controller, new Object[0]);
                }

            } catch (InvocationTargetException var3) {
                throw new RuntimeException(var3);
            } catch (IllegalAccessException var4) {
                throw new RuntimeException(var4);
            }
        }

        public CustomerFXMLLoader.SupportedType getType() {
            return this.type;
        }
    }
}
